package com.note.servlet.note;

import com.note.service.note.noteService;
import com.note.service.note.noteServiceImpl;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;

public class saveNote extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int userid = Integer.parseInt(req.getParameter("userid"));
        String title = req.getParameter("title");
        int kind = Integer.parseInt(req.getParameter("kind"));

        Timestamp time = Timestamp.valueOf(req.getParameter("time"));
        String content = req.getParameter("content");
        noteService save = new noteServiceImpl();
        boolean b = save.writeNote(title, content, kind, time, userid);
        if(b){
            resp.sendRedirect(req.getContextPath()+"/getType?index=1");
        }else{
            req.setAttribute("message","保存失败");
            req.getRequestDispatcher(req.getContextPath()+"/getType?index=1").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
